function loadLabResultStructure() {
	
	$.ajax({
		type:		"GET",
		url:		"../LabAdmin/LoadLabResultStructure?set_ajax_view",
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {
					$('#labTabContent').html(data.result);
				} else {
					displayError(data.error.mess);				
				}

			}

	});
}

function loadLabServicesOfSelectedGroupSec3(id, selectedLabserviceId, mess) {
	
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/ListLabServicesOfSelectedGroupAjax?set_ajax_view",
		data :   "labGroupId=" + id,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {
					var optionts = buildLabServicesSelectOptionSec3(data.result, id);
					$("select#labServiceSelectResultStructure").html(optionts);
					if (selectedLabserviceId != null) {						
						setSelectedLabServiceSec2(selectedLabserviceId);
					}
					showMessage('statusMessage', mess, 'success');
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
}
function buildLabServicesSelectOptionSec3(arrLabServices) {
	var option = "";
	for(var index in arrLabServices){
			option += "<option value='" + arrLabServices[index].service_id 
			+  "'  price='" + arrLabServices[index].price 
			+  "' disabledService='" +  arrLabServices[index].disabled
			+ "'>" 
			+ arrLabServices[index].name+ "</option>";
	}
	return option;
}
function loadLabServicesOfSelectedGroupSec3(labGroupId, selectedLabserviceId, mess) {
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/ListLabServicesOfSelectedGroupAjax?set_ajax_view",
		data :   "labGroupId=" + labGroupId,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {
					var optionts = buildLabServicesSelectOptionSec3(data.result);
					$("select#labServiceSelectResultStructure").html(optionts);										
										
					showMessage('statusMessage', mess, 'success');
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
}
function buildLabResultSelectOptionSec3(arrLabResult) {
	var option = "";
	for(var index in arrLabResult){
			option += '<option value="' + arrLabResult[index].lab_item_factor_id			
			+ '">'			
			+ arrLabResult[index].name
			+ " "
			+ _("[Normal(M): {0}; Normal(F): {1}; Unit: {2};Data type: {3}]",
			 arrLabResult[index].normal_result_male,
			 arrLabResult[index].normal_result_female,
			 arrLabResult[index].enum_unit_text,
			 arrLabResult[index].enum_data_type_text)
			+ "</option>";
	}
	return option;
}

function buildLabResultSelectHiddenSec3(arrLabResult) {
	var input = "";
	for(var index in arrLabResult){
		input += '<input type="hidden" id="labResultHidden' + arrLabResult[index].lab_item_factor_id			
			+ '"  result_name="' + arrLabResult[index].name
			+ '"  service_id="' + arrLabResult[index].service_id
			+ '"  enum_unit="' + arrLabResult[index].enum_unit
			+ '"  enum_data_type="' + arrLabResult[index].enum_data_type
			+ '"  normal_result_male="' + arrLabResult[index].normal_result_male
			+ '"  normal_result_female="' + arrLabResult[index].normal_result_female 
			+ '"'			
			+ "</input>";
	}
	return input;
}

function loadResultStructureOfSelectedService(labServiceId, selectedResultId, mess) {
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/ListLabResultOfSelectedServiceAjax?set_ajax_view",
		data :   "labServiceId=" + labServiceId,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {
					var optionts = buildLabResultSelectOptionSec3(data.result);
					$("select#labResultStructureSelect").html(optionts);		
					var hiddendData = buildLabResultSelectHiddenSec3(data.result);
					$("div#hiddenData").html(hiddendData);
					if (selectedResultId != null) {
						setSelectedLabResultStructure(selectedResultId);
					}
					showMessage('statusMessage', mess, 'success');
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
}



function setSelectedLabResultStructure(resultId) {
	$('#labResultStructureSelect :option').each(function() {
		if ($(this).val() == resultId) {
				$(this).attr('selected', 'selected');				
				$('#labResultName').val($('#labResultHidden' + resultId).attr('result_name'));
				$('#labResultId').val(resultId);
				$('#labResultNormalMale').val($('#labResultHidden' + resultId).attr('normal_result_male'));
				$('#labResultNormalFemale').val($('#labResultHidden' + resultId).attr('normal_result_female'));	
				$('#resultDataType').val($('#labResultHidden' + resultId).attr('enum_data_type'));
				$('#resultUnit').val($('#labResultHidden' + resultId).attr('enum_unit'));
				
		} else {
			$(this).attr('selected', false);
		}
		
	});
	
}

function setResultStructureDefaultData() {
	$('#labResultName').val($('#labServiceSelectResultStructure :selected').html());		
}


function resetLabResultData() {
	$('#labResultName').val('');
	$('#labResultId').val('');
	$('#labResultNormalMale').val('');
	$('#labResultNormalFemale').val('');	
	$('#labResultStructureSelect').html('');
	$('#labResultUpdate').attr('disabled', true);
	
}


function updateLabResult() {
	if (!labResultValidate()) {
		return;
	}
	var labServiceId = $('#labServiceSelectResultStructure :selected').val();
	var resultName = $('#labResultName').val();
	var resultId = $('#labResultId').val();
	var labResultNormalMale = $('#labResultNormalMale').val();
	var labResultNormalFemale = $('#labResultNormalFemale').val();
	var unit = $('#resultUnit').val();
	var dataType = $('#resultDataType').val();
	
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/UpdateLabResultAjax?set_ajax_view",
		data :   "resultId=" + resultId
				+"&resultName=" + resultName 
				+ "&labResultNormalMale=" +labResultNormalMale
				+ "&labResultNormalFemale=" +labResultNormalFemale
				+ "&unit=" + unit
				+ "&dataType=" + dataType,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {	
					loadResultStructureOfSelectedService(labServiceId, resultId, _('Save successful'));
					
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
	
	
}

function addLabResult() {
	if (!labResultValidate()) {
		return;
	}
	var labServiceId = $('#labServiceSelectResultStructure :selected').val();
	var resultName = $('#labResultName').val();
	var labResultNormalMale = $('#labResultNormalMale').val();
	var labResultNormalFemale = $('#labResultNormalFemale').val();
	var unit = $('#resultUnit').val();
	var dataType = $('#resultDataType').val();
	
	$.ajax({
		type:		"POST",
		url:		"../LabAdmin/AddLabResultAjax?set_ajax_view",
		data :   "labServiceId=" + labServiceId
				+"&resultName=" + resultName 
				+ "&labResultNormalMale=" +labResultNormalMale
				+ "&labResultNormalFemale=" +labResultNormalFemale
				+ "&unit=" + unit
				+ "&dataType=" + dataType,
		dataType:   "json",		
		success: 	function(data) {
		
				if (data.error.code ==  'OK') {	
					loadResultStructureOfSelectedService(labServiceId, data.result, _('Save successful'));					
				} else {
					displayError(data.error.mess);				
				}

			}

	});
	
	
	
}

function labResultValidate() {
	var name = $('#labResultName').val();
	if (name == null && $.trim(name) == '') {
		showMessage('statusMessage', _('Result name is empty'), 'error');
		return false;
	}	
	showMessage('statusMessage', '', 'success');
	return true;
}
